/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistance;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import shared.Memory;
import shared.Process;

/**
 *
 * @author xarckane
 */
public class DaoProcessus {

    private static DatabaseConnector connector = null;
    ;
    private static DaoProcessus instance=null;

    public static DaoProcessus getInstance() throws SQLException {
      
        if (null == instance) { // Premier appel
            instance = new DaoProcessus();
        }
        return instance;
    }

     void clear() throws SQLException {

        ResultSet resultat = connector.execQuery("select id from histo where type='process'");
        while (resultat.next()) {
            ResultSet resultat2 = connector.execQuery("select id_process from lnkhistoprocess where id_histo="+resultat.getInt("id"));
             connector.execUpdate("delete lnkhistoprocess where id_histo=" + resultat.getInt("id_histo"));
            while (resultat2.next()) {
            
             connector.execUpdate("delete process where id_process=" + resultat2.getInt("id_process"));
             
            }
            connector.execUpdate("delete from  histo where id=" + resultat.getInt("id"));

        }
    }

    private DaoProcessus() throws SQLException {
          connector = DatabaseConnector.getInstance();
    }

    public void saveProcess(String serv, shared.ProcessList process) throws SQLException {



        Integer lastinserted_idhisto = 0;
        Integer lastinserted_idprocess = 0;

        ResultSet resultat = connector.execUpdate("INSERT INTO histo (serv,type) VALUES (\"" + serv + "\",\"process\")");


        if (resultat.next()) {
            lastinserted_idhisto = resultat.getInt(1);
            resultat.close();

        } else {
            throw new SQLException("insertion ou récuperation de l'id impossible");
        }


        System.out.println(lastinserted_idhisto);
        for (shared.Process proc : process.getListe()) {
            resultat = connector.execUpdate("INSERT INTO process (nom,pid) VALUES (\"" + proc.getName() + "\"," + proc.getPid() + ")");



            if (resultat.next()) {
                lastinserted_idprocess = resultat.getInt(1);
                resultat.close();
            } else {

                throw new SQLException("insertion ou récuperation de l'id impossible");
            }



            System.out.println(lastinserted_idprocess);
            try {


                resultat = connector.execUpdate("INSERT INTO lnkhistoprocess (id_histo,id_process) VALUES (" + lastinserted_idhisto + "," + lastinserted_idprocess + ")");
            } catch (SQLException e) {
                connector.execUpdate("delete from process where id="+lastinserted_idprocess);
                System.out.println("delete from process where id="+lastinserted_idprocess);
                resultat= connector.execQuery("select id_process from lnkhistoprocess where id_histo=" + lastinserted_idhisto);
                if (!resultat.next()){
                    connector.execUpdate("delete from histo where id="+lastinserted_idhisto);
                }
                throw e;
                
            }
        }

    }

    public shared.ProcessList getProcessusByRequestId(int id) throws SQLException {

        System.out.println("select id_process from lnkhistoprocess where id_histo=" + id);
        ResultSet resultat = connector.execQuery("select id_process from lnkhistoprocess where id_histo=" + id);

        shared.ProcessList proc = new shared.ProcessList();
        proc.setListe(new ArrayList<Process>());


        while (resultat.next()) {
            System.out.println("select * from process where id=" + resultat.getInt("id_process"));
            ResultSet resultat2 = connector.execQuery("select * from process where id=" + resultat.getInt("id_process"));

            while (resultat2.next()) {
                proc.getListe().add(new shared.Process(resultat2.getString("nom"), resultat2.getInt("pid")));
            }
        }

        return proc;
    }
}
